package com.suntianci.stock.controller;

import com.suntianci.stock.pojo.domain.UserInfo4SelectById;
import com.suntianci.stock.pojo.domain.UserInfoDomain;
import com.suntianci.stock.pojo.entity.SysUser;
import com.suntianci.stock.service.UserService;
import com.suntianci.stock.vo.req.*;
import com.suntianci.stock.vo.resp.LoginRespVo;
import com.suntianci.stock.vo.resp.PageResult;
import com.suntianci.stock.vo.resp.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserController
 * Api开头的注解：swagger接口文档相关注解
 * @Author 孙天赐
 * @Date 2024/12/24 19:46
 * @Description TODO: 用户web层接口资源
 */

@RestController
@RequestMapping("/api")
@Api(tags = "用户相关接口处理")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * TODO: 根据用户名查询用户信息
     *
     * @param userName
     * @return
     */
    @ApiOperation(value = "根据用户名查询用户信息", notes = "根据用户名查询用户信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "string", required = true, type = "path")
    })
    @GetMapping("/user/{userName}")
    public SysUser findUserInfoByUserName(@PathVariable String userName) {
        return userService.findUserInfoByUserName(userName);
    }

    /**
     * TODO: 用户登录功能
     *
     * @param loginReqVo
     * @return
     */
    @ApiOperation(value = "用户登录功能", notes = "用户登录功能", httpMethod = "POST")
    @ApiImplicitParam(name = "loginReqVo", value = "登录信息请求数据封装类", dataType = "LoginReqVo", required = true, type = "query")
    @PostMapping("/login")
    public R<LoginRespVo> login(@RequestBody LoginReqVo loginReqVo) {
        return userService.login(loginReqVo);
    }

    /**
     * TODO: 生成图片验证码
     *
     * @return
     */
    @ApiOperation(value = "验证码生成", notes = "验证码生成", httpMethod = "GET")
    @GetMapping("/captcha")
    public R<Map> getCaptchaCode() {
        return userService.getCaptchaCode();
    }

    /**
     *  TODO: 分页查询全部用户信息
     * @param usersInfoReqVo
     * @return
     */
    @ApiOperation(value = "分页查询全部用户信息", notes = "分页查询全部用户信息", httpMethod = "POST")
    @ApiImplicitParam(name = "loginReqVo", value = "用户管理请求参数封装类", dataType = "UsersInfoReqVo", required = true, type = "query")
    @PostMapping("/users")
    @PreAuthorize("hasAnyAuthority('sys:user:list')")
    public R<PageResult<UserInfoDomain>> getUsersInfoByPage(@RequestBody UsersInfoReqVo usersInfoReqVo){
        return userService.getUsersInfoByPage(usersInfoReqVo);
    }

    /**
     * TODO: 添加用户信息
     * @param addUserReqVo
     * @return
     */
    @PostMapping("/user")
    @ApiOperation(value = "添加用户信息", notes = "添加用户信息", httpMethod = "POST")
    @ApiImplicitParam(name = "addUserReqVo", value = "添加用户请求实体Vo类", dataType = "AddUserReqVo", required = true, type = "query")
    @PreAuthorize("hasAnyAuthority('sys:user:add')")
    public R<Void> addUser(@RequestBody AddUserReqVo addUserReqVo, HttpServletRequest httpServletRequest){
        return userService.addUser(addUserReqVo, httpServletRequest);
    }

    /**
     * TODO: 获取用户具有的角色信息，以及所有角色信息
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取用户具有的角色信息，以及所有角色信息", notes = "获取用户具有的角色信息，以及所有角色信息", httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "用户id", dataType = "string", required = true, type = "path")
    @GetMapping("/user/roles/{userId}")
    @PreAuthorize("hasAnyAuthority('sys:user:list')")
    public R<Map<String, List>> getUserRoleAndAllRoles(@PathVariable String userId){
        return userService.getUserRoleAndAllRoles(userId);
    }

    /**
     * TODO: 更新用户角色信息
     * @Param updateUserRolesReqVo
     * @return
     */
    @ApiOperation(value = "更新用户角色信息", notes = "更新用户角色信息", httpMethod = "PUT")
    @ApiImplicitParam(name = "updateUserRolesReqVo", value = "修改用户角色请求参数", dataType = "UpdateUserRolesReqVo", required = true, type = "query")
    @PutMapping("/user/roles")
    @PreAuthorize("hasAnyAuthority('sys:user:update')")
    public R<Void> updateUserRoles(@RequestBody UpdateUserRolesReqVo updateUserRolesReqVo, HttpServletRequest httpServletRequest){
        return userService.updateUserRoles(updateUserRolesReqVo, httpServletRequest);
    }

    /**
     * TODO: 批量删除用户
     * @param userIds
     * @return
     */
    @ApiOperation(value = "批量删除用户信息", notes = "批量删除用户信息", httpMethod = "DELETE")
    @ApiImplicitParam(name = "userIds", value = "待删除用户的id列表", dataType = "List", required = true, type = "query")
    @DeleteMapping("/user")
    @PreAuthorize("hasAnyAuthority('sys:user:delete')")
    public R<Void> batchDeleteUser(@RequestBody List<Long> userIds){
        return userService.batchDeleteUser(userIds);
    }

    /**
     * TODO: 根据用户id查询用户信息
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户id查询用户信息", notes = "根据用户id查询用户信息", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "用户id", dataType = "string", required = true, type = "path")
    @GetMapping("/user/info/{userId}")
    public R<UserInfo4SelectById> getUserInfoById(@PathVariable String userId){
        return userService.getUserInfoById(userId);
    }

    /**
     * TODO: 根据id更新用户基本信息
     * @param updateUserReqVo
     * @return
     */
    @ApiOperation(value = "根据id更新用户基本信息", notes = "根据id更新用户基本信息", httpMethod = "PUT")
    @ApiImplicitParam(name = "updateUserReqVo", value = "用户信息封装", dataType = "UpdateUserReqVo", required = true, type = "query")
    @PutMapping("/user")
    public R<Void> updateUserById(@RequestBody UpdateUserReqVo updateUserReqVo, HttpServletRequest httpServletRequest){
        return userService.updateUserById(updateUserReqVo, httpServletRequest);
    }

}
